package com.siwei.mes.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.config.UidUtils;
import com.siwei.mes.entity.Equipment;
import com.siwei.mes.enums.GenerateCodeEnum;
import com.siwei.mes.mapper.EquipmentMapper;
import com.siwei.mes.service.EquipmentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 设备表(Equipment)表服务实现类
 *
 * @author linzi
 * @since 2023-11-25 22:22:21
 */
@Service
public class EquipmentServiceImpl implements EquipmentService {
    @Resource
    private EquipmentMapper equipmentMapper;

    @Override
    public Long create(Equipment entity) {
        entity.init(true);
        if (StringUtils.isBlank(entity.getEquipmentNo())) {
            // 如果设备编号为空 自动生成一个
            entity.setEquipmentNo(UidUtils.genNo(GenerateCodeEnum.EQUIPMENT.getCode()));
        }
        /**判断设备名称是否存在*/
        assertTrue(equipmentMapper.selectByEquipmentName(entity.getEquipmentName()) != null, "该设备名称已存在");
        equipmentMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(Equipment entity) {
        Equipment equipment = equipmentMapper.selectByPrimaryKey(entity.getId());
        assertTrue(equipment == null, "设备表信息不存在");
        if (!entity.getEquipmentName().equals(equipment.getEquipmentName())) {
            /**判断设备名称是否存在*/
            assertTrue(equipmentMapper.selectByEquipmentName(entity.getEquipmentName()) != null, "该设备名称已存在");
        }
        entity.init(false);
        equipmentMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        Equipment equipment = equipmentMapper.selectByPrimaryKey(id);
        assertTrue(equipment == null, "设备表信息不存在");
        equipmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Equipment> getPage(PageQuery<Equipment, Equipment> query) {
        Equipment searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> equipmentMapper.getList(searchParam));
    }

    @Override
    public Equipment loadById(Long id) {
        return equipmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Equipment> getAll(Equipment entity) {
        return equipmentMapper.getList(entity);
    }

    @Override
    public List<Equipment> getEquipmentInfo(String testProjectType) {
        List<Equipment> list = new ArrayList<>();
        List<Equipment> equipmentList = equipmentMapper.getEquipmentInfo(testProjectType);
        /**这里还要过滤一下 因为是模糊查询的 所有会不对*/
        for (Equipment equipment : equipmentList) {
            JSONArray testProjectJson = equipment.getTestProjectJson();
            for (Object o : testProjectJson) {
                JSONObject jsonObject = (JSONObject) o;
                if (jsonObject.get("no").equals(testProjectType)) {
                    list.add(equipment);
                }
            }
        }
        return list;
    }
}
